#include "kernel_operator.h"
// tensor num for each queue
constexpr int32_t BUFFER_NUM = 2;
constexpr int32_t DEPTH = 47;

class KernelOnehot {
public:
    __aicore__ inline KernelOnehot() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, uint32_t smallCoreDataNum,
                                uint32_t bigCoreDataNum, uint32_t finalBigTileNum, 
                                uint32_t finalSmallTileNum, uint32_t tileDataNum, 
                                uint32_t smallTailDataNum, uint32_t bigTailDataNum, 
                                uint32_t tailBlockNum) 
    {
        ASSERT(AscendC::GetBlockNum() != 0 && "block dim can not be zero!");
        uint32_t coreNum = AscendC::GetBlockIdx();
        uint32_t globalBufferIndex = bigCoreDataNum * AscendC::GetBlockIdx();
        this->tileDataNum = tileDataNum;
        this->DEPTH_align =  ((DEPTH + 8 - 1) / 8) * 8;
        this->mask_cmp = DEPTH_align;
        if (coreNum < tailBlockNum) { 
          this->coreDataNum = bigCoreDataNum;
          this->tileNum = finalBigTileNum;
          this->tailDataNum = bigTailDataNum;
        }
        else { 
          this->coreDataNum = smallCoreDataNum;
          this->tileNum = finalSmallTileNum;
          this->tailDataNum = smallTailDataNum;
          globalBufferIndex -= (bigCoreDataNum - smallCoreDataNum) * (AscendC::GetBlockIdx() - tailBlockNum);
        }
        xGm.SetGlobalBuffer((__gm__ int32_t*)x + globalBufferIndex, this->coreDataNum);
        zGm.SetGlobalBuffer((__gm__ int32_t*)z + globalBufferIndex*DEPTH, this->coreDataNum*DEPTH);

        pipe.InitBuffer(cmpBuf, 8*DEPTH_align * sizeof(uint8_t));

        pipe.InitBuffer(oneBuf, DEPTH_align * sizeof(float));
        //pipe.InitBuffer(oneBuf, this->tileDataNum*DEPTH * sizeof(float));
        pipe.InitBuffer(zBuf, DEPTH_align * sizeof(float));
        pipe.InitBuffer(dBuf, DEPTH_align * sizeof(int32_t));

        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileDataNum * sizeof(int32_t));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, DEPTH_align * sizeof(int32_t));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
          if (i == this->tileNum - 1) {
              this->processDataNum = this->tailDataNum;
            }
          for (int32_t j = 0; j<this->processDataNum; j++) {
            CopyIn(i,j);
            Compute(i,j);
            CopyOut(i,j);
          }
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress,int32_t j)
    {
      AscendC::LocalTensor<int32_t> xLocal = inQueueX.AllocTensor<int32_t>();
      AscendC::DataCopy(xLocal, xGm[progress * this->tileDataNum], this->processDataNum);
      inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(int32_t progress,int32_t j)
    {
      AscendC::LocalTensor<int32_t> xLocal = inQueueX.DeQue<int32_t>();
      AscendC::LocalTensor<int32_t> zLocal = outQueueZ.AllocTensor<int32_t>();
      AscendC::LocalTensor<uint8_t> cmp = cmpBuf.AllocTensor<uint8_t>();
      AscendC::LocalTensor<float> zfloat = zBuf.AllocTensor<float>();
      AscendC::LocalTensor<float> oneLocal = oneBuf.AllocTensor<float>();
      AscendC::LocalTensor<int32_t> dLocal = dBuf.AllocTensor<int32_t>();
      //AscendC::Duplicate(oneLocal, 1.0f, this->processDataNum*DEPTH);
      AscendC::Duplicate(oneLocal, 1.0f, DEPTH_align);
      for(int i=0;i<DEPTH;i++){
        dLocal.SetValue(i, i);
      }
      for(int i=DEPTH;i<DEPTH_align;i++){
        dLocal.SetValue(i, -1);
      }
       int32_t xVal = xLocal.GetValue(j);
      AscendC::CompareScalar(cmp, dLocal, xVal, AscendC::CMPMODE::EQ, mask_cmp, repeat, repeatParams);
      AscendC::LocalTensor<uint16_t> mask_Dup = cmpBuf.AllocTensor<uint16_t>();
      AscendC::Select(zfloat, mask_Dup[0], oneLocal, 0.0f, AscendC::SELMODE::VSEL_TENSOR_SCALAR_MODE, DEPTH);
      AscendC::Cast(zLocal, zfloat, AscendC::RoundMode::CAST_CEIL,DEPTH);
      outQueueZ.EnQue<int32_t>(zLocal);
      inQueueX.FreeTensor(xLocal);
      cmpBuf.FreeTensor(cmp);
      oneBuf.FreeTensor(oneLocal);
      zBuf.FreeTensor(zfloat);
      dBuf.FreeTensor(dLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress,int32_t j)
    {
        AscendC::LocalTensor<int32_t> zLocal = outQueueZ.DeQue<int32_t>();
        AscendC::DataCopyExtParams copyParams{static_cast<uint16_t>(1), static_cast<uint32_t>(DEPTH * sizeof(int32_t)), 0, 0, 0};
        AscendC::DataCopyPad(zGm[(progress * this->tileDataNum+j)*DEPTH], zLocal, copyParams);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::GlobalTensor<int32_t> xGm;
    AscendC::GlobalTensor<int32_t> zGm;

    AscendC::TBuf<AscendC::QuePosition::VECCALC> cmpBuf;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> oneBuf;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> zBuf;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> dBuf;

    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;

    int32_t DEPTH_align;

    uint64_t mask_cmp;
    int repeat = 1;

    AscendC::UnaryRepeatParams repeatParams = { 1, 1, 8, 8 };
};
extern "C" __global__ __aicore__ void onehot_custom(GM_ADDR x, GM_ADDR z, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    KernelOnehot op;
    op.Init(x, z, tiling_data.smallCoreDataNum, 
            tiling_data.bigCoreDataNum, tiling_data.finalBigTileNum, 
            tiling_data.finalSmallTileNum, tiling_data.tileDataNum, 
            tiling_data.smallTailDataNum, tiling_data.bigTailDataNum, 
            tiling_data.tailBlockNum);  
    op.Process();
}
